<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 品类模型
 */

class ConfCategoryModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('conf_category', 'require', '品类名称不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('conf_category', 'checkName', '品类名称已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('conf_category', '1,20', '品类名称长度不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('sort', 'number', '排序必须为数字', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('sort', '1,5', '排序长度不能大于5个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_BOTH),
    	array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 检测名称是否存在
     * @param  string $login_name 用户名
     * @return boolean          ture - 未重名，false - 已重名
     */
    protected function checkName($conf_category){
    	$conf_category_id = (int)I('conf_category_id');
    	//判断是否存在
    	$map = array();
    	if($conf_category_id != 0){
    		$map['conf_category_id'] = array('neq',$conf_category_id);
    	}
    	$map['conf_category'] = $conf_category;
    	$map['status'] = array('gt',-1);
    	if($this->where($map)->find()){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 新增或更新一个品类
     * @return boolean fasle 失败 ， int  成功 返回品类ID
     * @author sap
     * @since 2015-8-17
     */
    public function update(){
        /* 获取数据对象 */
    	$data = $this->create($_POST);
        if(empty($data)){
            return false;
        }
        
        /* 添加或更新数据 */
        if(empty($data['conf_category_id'])){ //新增数据
            $conf_category_id = $this->add();
            if(!$conf_category_id){
                $this->error = '新增品类出错！';
                return false;
            }
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新品类出错！';
                return false;
            }else{
            	$conf_category_id = $data['conf_category_id'];
            }
        }
        S('sys_conf_category_',null);//清除缓存
        //内容添加或更新完成
        return $conf_category_id;
    }

    /**
     * @todo 品类详情
     * @param int $conf_category_id 品类id
     * @param string $field 查询字段
     * @return false|array
     * @author sap
     */
    public function confCategoryShow($conf_category_id=0,$field=''){
    	//获取数据
    	$conf_category_id = (int)$conf_category_id;
    	if($conf_category_id == 0){
    		$this->error = '请正确操作';
    		return false;
    	}
    	$map = array();//初始化查询条件
    	//查询条件
    	$map['conf_category_id'] = $conf_category_id;
    	$map['status'] = array('gt',-1);
    	//返回值
    	return $this->where($map)->field($field)->find();
    }
    
    /**
     * @todo 品类列表
     * @param array $where 查询条件数组
     * @param string $field 查询字段
     * @param string $orderby 排序
     * @return false|array 品类列表数组
     * @author sap
     */
    public function getConfCategoryList($where = array(),$field= '',$orderby='sort desc,conf_category_id desc'){
    	return $this->where($where)->field($field)->order($orderby)->select();
    }
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
     */
    public function getPk(){
    	return 'conf_category_id';
    }
}
